package cn.com.xiaoshoujia.activities.service.impl;

import cn.com.xiaoshoujia.activities.activity.ActivitiesActivity;
import cn.com.xiaoshoujia.activities.dto.*;
import cn.com.xiaoshoujia.activities.entity.Activity;
import cn.com.xiaoshoujia.activities.entity.ActivityRules;
import cn.com.xiaoshoujia.activities.service.ActivitiesService;
import cn.com.xiaoshoujia.activities.vo.ActivityDetailSiteVO;
import cn.com.xiaoshoujia.activities.vo.ActivitySiteVO;
import cn.com.xiaoshoujia.user.entity.ActivityTeamHead;
import cn.com.xiaoshoujia.utils.DateUtils;
import cn.com.xiaoshoujia.utils.QueryResult;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ActivitiesServiceImpl implements ActivitiesService {

    @Autowired
    private ActivitiesActivity activitiesActivity;

    @Override
    public boolean saveTeamActivity(ActivitySiteDTO activitySiteDTO) {
        return activitiesActivity.saveTeamActivity(activitySiteDTO);
    }

    /**
     * @Author: yangrui
     * @Description: 筛选
     * sort 1 最新创建，2 最多报名；
     * screen 1 进行中，2 已过期，3 已暂停
     * @Date: 下午2:40 2017/7/31
     */
    @Override
    public QueryResult findByPage(ActivitySiteVO activitySiteVO) {

        if (!StringUtils.isEmpty(activitySiteVO.getSort())) {
            String sort = activitySiteVO.getSort();
            switch (sort) {
                case "1":
                    sort = "createTime";
                    break;
                case "2":
                    sort = "count";
                    break;
            }
            activitySiteVO.setSort(sort);
        } else {
            activitySiteVO.setSort("createTime");
        }

        if (!StringUtils.isEmpty(activitySiteVO.getScreen())) {
            String screen = activitySiteVO.getScreen();
            switch (screen) {
                case "1":
                    activitySiteVO.setActivitiesStatus(0);
                    break;
                case "2":
                    String overTime = DateUtils.getCurrentTime();
                    activitySiteVO.setOverTime(overTime);
                    break;
                case "3":
                    activitySiteVO.setActivitiesStatus(1);
                    break;
            }
        }
        return activitiesActivity.findByPage(activitySiteVO);
    }

    @Override
    public boolean updateStatus(Activity activity) {
        return activitiesActivity.updateStatus(activity);
    }

    @Override
    public boolean delete(Activity activity) {
        return activitiesActivity.delete(activity);
    }

    @Override
    public boolean update(Long id, ActivitySiteDTO activitySiteDTO) {
        Activity activity = new Activity();
        activity.setId(id);
        BeanUtils.copyProperties(activitySiteDTO, activity);
        ActivityRules activityRules = new ActivityRules();
        activityRules.setFkActivitiesId(id);
        BeanUtils.copyProperties(activitySiteDTO, activityRules);
        return activitiesActivity.update(activity, activityRules);
    }

    @Override
    public ActivityDetailSiteVO findById(Long id) {
        return activitiesActivity.findById(id);
    }

    /**
     * @Author: dulei
     * @Description: 查询拼团活动集合
     * @param: xcode 企业唯一编码
     * @Date: 2017/8/2 14:25
     */
    public List<ActivityDTO> listTeam(String xcode, Long id) {
        return activitiesActivity.listTeam(xcode, id);
    }

    @Override
    public Activity findWxStatusById(Long id) {
        return activitiesActivity.findWxStatusById(id);
    }

    /*--------------------wx start--------------------*/

    /**
     * @Author: dulei
     * @Description: 提供拼团活动信息
     * @param: id 拼团活动ID
     * @Date: 2017/8/3 17:20
     */
    public ActivityWxTeamMsgDTO findWxActivityMsg(Long id) {

        return activitiesActivity.findWxActivityMsg(id);
    }

    /**
     * @Author: dulei
     * @Description: 查询表单信息及返回团，活动状态
     * @param: id 拼团互动ID headId 团ID
     * @Date: 2017/8/4 10:19
     */
    public ActivityOperationJsonDTO findOperaionJson(Long id, Long headId) {
        return activitiesActivity.findOperaionJson(id, headId);
    }

    @Override
    public ActivityWxActivityMsgDTO findTeamAndHead(Long id, Long headId) {
        return activitiesActivity.findTeamAndHead(id, headId);
    }

    @Override
    public ActivityTeamHead findWxHeadById(Long headId) {
        return activitiesActivity.findWxHeadById(headId);
    }

    @Override
    public ActivitySuccessDTO findSuccessMsg(Long id, Long headId) {
        return activitiesActivity.findSuccessMsg(id,headId);
    }
    /*--------------------wx end----------------------*/
}
